﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Linq;

public partial class MasterLogin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            cboUsers.DataSource = Membership.GetAllUsers().Cast<MembershipUser>().Select(m => m.UserName).ToList();
            cboUsers.DataBind();
            cboUsers.ClearSelection(); 
            //cmdLogin_Click(null, null);
        }
    }
    protected void cmdLogin_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            if (Membership.ValidateUser(Membership.GetUserNameByEmail(cboUsers.SelectedValue.Trim()), txtPassword.Text.Trim()) == true)
            {

                string[] strRoleName;
                FormsAuthentication.RedirectFromLoginPage(Membership.GetUserNameByEmail(cboUsers.SelectedValue.Trim().ToString()).ToString(), false);
                strRoleName = Roles.GetRolesForUser(Membership.GetUserNameByEmail(cboUsers.SelectedValue.Trim().ToString()).ToString());
                Session["UserName"] = cboUsers.SelectedValue.Trim();
                Response.Redirect("~/home/DashBoard.aspx");
                //switch (strRoleName[0])
                //{
                //    case "SU":
                //        Response.Redirect("~/home/Dashboard.aspx");
                //        break;
                //    case "GD":
                //        Response.Redirect("~/regionaldirector/rdhome_1.aspx");
                //        break;
                //    case "RD":
                //        Response.Redirect("~/regionaldirector/rdhome_1.aspx");
                //        break;
                //    case "RARL":
                //        Response.Redirect("~/ResearchLead/rdhome_1.aspx");
                //        break;
                //    case "RAM":
                //        Response.Redirect("~/Moderator/rdhome_1.aspx");
                //        break;
                //    case "AD":
                //        Response.Redirect("~/Auditor/rdhome_1.aspx");
                //        break;
                //    case "IE":
                //        Response.Redirect("~/IndExpert/rdhome_1.aspx");
                //        break;
                //    case "IQKSAD":
                //        //Response.Redirect("~/IQManager/rdhome_1.aspx");
                //        Response.Redirect("home.aspx");
                //        break;
                //    default:
                //        Response.Redirect("~/Candidate/CandidateBrief.aspx");
                //        break;
                //}

            }
            else
            {
                // Determine why the user could not login...
                ErrorMsg.Text = "Invalid Email ID or Password. Please try again.";

                // Does there exist a User account for this user?
                string str = Membership.GetUserNameByEmail(cboUsers.SelectedValue.Trim());

                if (str != null)
                {
                    MembershipUser usrInfo = Membership.GetUser(Membership.GetUserNameByEmail(cboUsers.SelectedValue.Trim()));
                    if (usrInfo != null)
                    {
                        // Is this user locked out?
                        if (usrInfo.IsLockedOut)
                        {
                            ErrorMsg.Text = "Your account has been locked, Kindly use forgot password to generate your new password.";
                        }
                        else if (!usrInfo.IsApproved)
                        {
                            ErrorMsg.Text = "Your account has not yet been approved. You cannot login until an administrator has approved your account.";
                        }
                    }
                }
                else
                {
                    ErrorMsg.Text = "Your Email ID is not yet registered. Please proceed with new Registration.";
                }
            }
        }
    }
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {

    }

    protected void Btnregister_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Common/CandidateRegistration.aspx");
    }
    protected void Btnalreadyregister_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Common/LoginAlready.aspx");
    }
}